package com.yc.web.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.yc.bean.Cartinfo;
import com.yc.biz.AddrBiz;
import com.yc.biz.CartinfoBiz;
import com.yc.biz.OrderBiz;
import com.yc.biz.impl.AddrBizImpl;
import com.yc.biz.impl.CartinfoBizImpl;
import com.yc.biz.impl.OrderBizImpl;

@WebServlet("/orderServlet")
public class OrderServlet extends HttpServlet {

	OrderBiz ob=new OrderBizImpl();
	CartinfoBiz cb=new CartinfoBizImpl();
	AddrBiz ab=new AddrBizImpl();
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doPost(req,resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		PrintWriter out=resp.getWriter();
		
		String op=req.getParameter("op");
		if("addOrder".equals(op)){
			addOrder(req,resp,out);
		}else if("findAllOrder".equals(op)){
			findAllOrder(req,resp,out);
		}else if("payOrder".equals(op)){
			payOrder(req,resp,out);
		}else if("findMoney".equals(op)){
			findMoney(req,resp,out);
		}else if("findPayPwd".equals(op)){
			findPayPwd(req,resp,out);
		}
	}
	
	//判断支付密码是否为空
		private void findPayPwd(HttpServletRequest req, HttpServletResponse resp, PrintWriter out) {
			int uid=Integer.parseInt(req.getSession().getAttribute("uid")+"");
			String paypwd=ob.findPayPwd(uid);
			
			if(paypwd==null || paypwd=="" || paypwd.equals("")){
				out.print(0);
				System.out.println("paypwd为空");
			}else{
				out.print(1);
			}
		}


	//判断余额是否充足
	private void findMoney(HttpServletRequest req, HttpServletResponse resp, PrintWriter out) {
		int uid=Integer.parseInt(req.getSession().getAttribute("uid")+"");
		String oid=req.getParameter("oid");
		double money=ob.findMoneyByPtid(uid, oid);
		out.print(money);
	}

	//判断支付密码是否正确
	private void payOrder(HttpServletRequest req, HttpServletResponse resp, PrintWriter out) {
		int uid=Integer.parseInt(req.getSession().getAttribute("uid")+"");
		String paypwd=req.getParameter("paypwd");
		double price=Double.parseDouble( req.getParameter("price") );
		String oid=req.getParameter("oid");
		
		int result=ob.payOrder(uid, paypwd);
		if(result>0){
			//修改成功
			//调用函数修改订单表的状态  修改用户的余额
			int result1=ob.updateStatus(uid, oid, price);
			if(result1>0){
				out.print(1);
			}else{
				out.print(0);
			}
		}else{
			
			out.print(0);
			
		}
	}

	//查询所有的订单
	private void findAllOrder(HttpServletRequest req, HttpServletResponse resp, PrintWriter out) {
		int uid=Integer.parseInt(req.getSession().getAttribute("uid")+"");
		int page=Integer.parseInt(req.getParameter("page"));
		
		List<Map<String, String>> list=ob.findAllOrder(uid,page);
		if(list.isEmpty()){
			out.print(0);
			return;
		}
		
		Gson gson=new Gson();
		out.print(gson.toJson(list));
	}

	//添加订单
	private void addOrder(HttpServletRequest req, HttpServletResponse resp, PrintWriter out) {
		//订单表需要订单号null,uid:用户号,aid:地址的id，gid:商品号 ,date:日期,allprice:此次订单的金额,支付状态 ptid:付款方式
		int uid=Integer.parseInt(req.getSession().getAttribute("uid")+"");
		List<Map<String,String>> list=ab.getDafalutAddr(uid);
		if(list.isEmpty()){
			out.print(-2);	//默认地址为空
			return;
		}
		int aid=Integer.parseInt(list.get(0).get("aid")+"");
		//获取时间
		Date d=new Date();
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String date=sdf.format(d);
		
		int length=Integer.parseInt(req.getParameter("length"));
		String str=req.getParameter("str");
		String strMoney=req.getParameter("strMoney");
		String strNum=req.getParameter("strNum");
		
		
		int ptid=Integer.parseInt(req.getParameter("ptid"));
		
		int result=ob.addOrder(uid, ptid, aid, str,strNum, date,strMoney);
		if(result>0){
			//更新购物车的数量
			int cartNum=cb.findCartNum(uid);
			req.getSession().setAttribute("cartNum", cartNum);
			
			//更新购物车信息
			List<Cartinfo> cartinfo=cb.findAll(uid);
			req.getSession().setAttribute("cartinfo", cartinfo);
			out.print(1);
		}else{
			out.print(0);
		}
	}

}
